/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package servlets;

import MiniProjet.gestionnaires.GestionnaireFilms;
import MiniProjet.gestionnaires.GestionnairePanier;
import MiniProjet.modeles.Film;
import MiniProjet.modeles.LigneCommande;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author SaLaH
 */
@WebServlet(name="ServletPanier", urlPatterns={"/ServletPanier"})
public class ServletPanier extends HttpServlet {
   
    @EJB
    private GestionnaireFilms gestionnaireFilms;
    
    @EJB
    private GestionnairePanier gestionnairePanier;
     
     
    public static final int QUANTITY_ERROR_CODE = 1;
    public static final int TYPE_ERROR_CODE = 2;
    public static final int REMAIN_QUANTITY_ERROR_CODE = 3;


    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        RequestDispatcher dp;
        int error = -1;
        boolean redirect = false;
        String filmIdStr = request.getParameter("filmId");
        String quantiteStr = "1";
     
        if (filmIdStr != null) {
            int filmId = Integer.valueOf(filmIdStr);
            Film film = gestionnaireFilms.getFilmById(filmId);
            request.setAttribute("film", film);
          
            if ((quantiteStr==null)&&(quantiteStr.trim().length() < 0)) {
                error = QUANTITY_ERROR_CODE;
            }
          /*
           else if (Integer.valueOf(quantiteStr) > gestionnaireFilms.getFilmById(filmId).getQuantite()) {
                error = REMAIN_QUANTITY_ERROR_CODE;
            }
            */
            else {
                redirect = true;
                 Integer.parseInt(quantiteStr);
                int quantite = Integer.valueOf(quantiteStr);
                gestionnairePanier.addligneCommande(filmId, quantite);
            }
        }

      request.getSession(true).setAttribute("MyStatefullCartProcessBean", gestionnairePanier);
       Collection<LigneCommande> lignesCommandes = gestionnairePanier.getligneCommandeList();
       request.getSession(true).setAttribute("lignesCommandes", lignesCommandes);
       request.getSession(true).setAttribute("var", quantiteStr);
        request.getSession(true).setAttribute("total", gestionnairePanier.getTotalPriceLineItemList());

        if (error > 0) {
            request.setAttribute("errorCode", error);
            dp = request.getRequestDispatcher("addFilm.jsp");
        } else {
            dp = request.getRequestDispatcher("panier.jsp");
        }

        if (redirect) {
           // response.sendRedirect(request.getContextPath() + "/ServletPanier");
            dp.forward(request, response);
        } else {
            dp.forward(request, response);
        }
    

 
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
